<?
//include script to do user logins
include("login/authincludesessions.php");
?>

<html>
<?

include("header.php");

?>

<div id="leftpane">

<?
//include script to do user logins
include("login/authincludemain.php");
?>


<?php

//include db access
include("includes/auth.php");
include("includes/dbconnect.php");

//get variable from url
$ulat= $_GET['ulat'];
$ulong= $_GET['ulong'];
$uname= $_GET['uname'];
$uownerid =  $_GET['uownerid'];
$ustatus =  $_GET['ustatus'];
$udescription =  $_GET['udescription'];
$uaddress =  $_GET['uaddress'];
$uurl =  $_GET['uurl'];

//Round off input numbers as stored in MySQL to 6 decimal places
//anyway, so need to make same for comparisons later
$ulat = round($ulat,6);
$ulong = round($ulong,6);

//set error found variable to false to begin with
$error_found=false;


if(!$session->logged_in)
{
$error_found=true;
echo("You must log in to add a new user<p>");
}

//Do some fancy coding to check if co-ords are out of SA range, so we
//don't get nodes added for Finland

$salat_min = -39;
$salat_max = -25;
$salong_min = 125;
$salong_max = 142;

if(($ulat< $salat_min || $ulat > $salat_max) || ($ulong < $salong_min || $ulong > $salong_max) || $uname=='')
      {
      //print what the problem was
      if($ulat<$salat_min) echo ("Latitude too small ($ulat), needs to be > $salat_min<br>");
      if($ulat>$salat_max) echo ("Latitude too large ($ulat), needs to be < $salat_max<br>");
      if($ulong<$salong_min) echo ("Longitude too small ($ulong), needs to be > $salong_min<br>");
      if($ulong>$salong_max) echo ("Longitude too large ($ulong), needs to be < $salong_max<br>");
      if($uname=='') echo ("Please insert a username<br>");
      echo("<b>Data not entered into database</b><p>");
      }

//Check if co-ords already in database

      $result = mysql_query("SELECT * FROM nodes",$link);
      if (!$result)
      {
              echo "no results ";
      }
      while($row = mysql_fetch_array($result))
      {

              if($row['lon']==$ulong && $row['lat']==$ulat)
              {
                      echo ("<b>Error:</b><br>Those co-ordinates have already been registered for user " . $row['name'] . " <br>");
                      //set variable to say dupe found
                      $error_found = true;
                      //break as no need to continue
                      break;
              }

      }


//add to database if no duplicate co-ords found

if(!$error_found)
{
//insert data into database
      mysql_query("INSERT INTO nodes VALUES('NULL', '$ulat','$ulong', '$uname', '$uownerid', '$ustatus', '$uabout', '$uaddress','$uurl' ) ",$link)
      or die(mysql_error());

//Get the value of the row we just inserted into
$nodeid = mysql_insert_id();

      //And echo back inserted data
      echo 'Node ID: ' . $nodeid . '<br>' . "\n";
      echo 'Successfully inserted data: <br>' . "\n";
      echo 'Name: ' . $uname . '<br>' . "\n";
      echo 'Latitude: ' . $ulat . '<br>' . "\n";
      echo 'Longitude: ' . $ulong . '<br>' . "\n";
      echo 'ownerid: ' . $uownerid . '<br>' . "\n";
      echo 'status: ' . $ustatus . '<br>' . "\n";
      echo 'description: ' . $udescription . '<br>' . "\n";
      echo 'address: ' . $uaddress . '<br>' . "\n";
      echo 'url: ' . $uurl . '<br>' . "\n";
}

//close off database connection
mysql_close($link);

?>

<a href="index.php">Usermap Home</a>

<div id="leftpane">